package demo;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import mysqlCodeGen.MyCusConfig;
import mysqlCodeGen.gen.Gen;
import mysqlCodeGen.gen.handler.ReplaceHandler;
import mysqlCodeGen.gen.handler.impl.ApiExportReplaceHandler;
import mysqlCodeGen.gen.handler.impl.ApiReplaceHandler;
import mysqlCodeGen.gen.handler.impl.BeanReplaceHandler;
import mysqlCodeGen.gen.handler.impl.XmlReplaceHandler;
import mysqlCodeGen.gen.inner.CodeConfig;
import mysqlCodeGen.gen.utils.PropUtil;

import java.util.List;

/**
 * @author loser
 * @date 2023-01-06  14:26
 */
public class DemoGenMysqlCode {

    public static void main(String[] args) {
        genCode();
    }

    public static void genCode() {

        try {
            // 01 获取自定义配置
            MyCusConfig cusConfig = PropUtil.loadStrValue(MyCusConfig.class, "genCodeConfig.properties", "customer");
            String dbName = cusConfig.getDbName();
            String dbUrl = cusConfig.getDbUrl();
            String dbUser = cusConfig.getDbUser();
            String dbPw = cusConfig.getDbPw();
            String tables = cusConfig.getTables();
            String module = cusConfig.getModule();
            String api = cusConfig.getApi();
            String author = cusConfig.getAuthor();
            boolean removePre = "true".equals(cusConfig.getRemovePre());
            String driver = cusConfig.getDriver();

            // 02 添加模板中的替换值
            CodeConfig codeConfig = new CodeConfig();
            codeConfig.append("author", author)
                    .append("genDate", DateUtil.now())
                    .append("module", module)
                    .append("api", api)
                    .append("dbPre", build(dbName))
                    .append("transactionManager", buildTm(dbName))
            ;
            for (String table : tables.split(",")) {
                List<ReplaceHandler> handlers = Lists.newArrayList();
                handlers.add(new BeanReplaceHandler(table, dbUrl, dbUser, dbPw, dbName, removePre, driver));
                handlers.add(new XmlReplaceHandler(table, dbUrl, dbUser, dbPw, dbName, driver));
                handlers.add(new ApiReplaceHandler(table, dbUrl, dbUser, dbPw, dbName, driver));
                handlers.add(new ApiExportReplaceHandler(table, dbUrl, dbUser, dbPw, dbName, driver));
                Gen.genFile(handlers, codeConfig, DemoMysqlMapperConfig.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static String build(String dbName) {
        switch (dbName) {
            case "activity":
                return "act";
            case "me":
                return "me";
            case "popo":
                return "popo";
            case "statistics":
                return "stat";
        }
        return dbName;
    }

    private static String buildTm(String dbName) {
        switch (dbName) {
            case "activity":
                return "Act";
            case "me":
                return "Me";
            case "popo":
                return "Popo";
            case "statistics":
                return "Stat";
        }
        return dbName;
    }


}
